<?php
/**
 * This is the model class for table "{{browser}}".
 *
 * The followings are the available columns in table '{{browser}}':
 * @property integer $id
 * @property string $module
 * @property integer $module_id
 * @property string $user_agent
 * @property string $browser
 * @property string $version
 * @property string $country_code
 * @property string $host
 * @property string $ip_address
 * @property string $x_forwarded_for
 * @property string $create_timezone
 * @property string $create_timestamp
 * @property string $create_datetime
 * @property string $create_date
 * @property string $create_time
 * @property integer $create_by
 * @property string $update_timezone
 * @property string $update_timestamp
 * @property string $update_datetime
 * @property string $update_date
 * @property string $update_time
 * @property integer $update_by
 */
class BrowserLog extends ActiveRecord {

    public $module_;
    public $moduleId_;
    public $scenario_;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Browser the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{browser_log}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('module_, moduleId_, scenario_', 'safe', 'on' => 'register'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('int', $this->int);
        $criteria->compare('module_id', $this->module_id);
        $criteria->compare('module', $this->module, true);
        $criteria->compare('user_id', $this->user_id);
        $criteria->compare('user_agent', $this->user_agent, true);
        $criteria->compare('browser', $this->browser, true);
        $criteria->compare('version', $this->version, true);
        $criteria->compare('country', $this->country, true);
        $criteria->compare('host', $this->host, true);
        $criteria->compare('ip_address', $this->ip_address, true);
        $criteria->compare('x_forwarded_for', $this->x_forwarded_for, true);

        return new ActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    protected function beforeSave() {
        if (parent::beforeSave()) {            

            if ($this->isNewRecord) {
                $this->module = strtolower($this->module_);
                $this->module_id = $this->moduleId_;
            
                $browser = new Browser();
                $this->user_agent = $browser->getUserAgent(); //Yii::app()->request->userAgent;
                $this->browser = $browser->getBrowser();
                $this->version = $browser->getVersion();
                $this->ip_address = $browser->getIP(); //IP Address //Yii::app()->request->userHostAddress;
                $this->x_forwarded_for = $browser->getIP(); // XForwarded
                $this->host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null;
                $this->country_code = Yii::app()->controller->countryCode; //Environment::isLocal() ? null : Yii::app()->geoip->lookupCountryCode(Yii::app()->request->userHostAddress);

                $this->setCreateDateTime();
            } else {
                $this->setUpdateDateTime();
            }
            return true;
        }
        else
            return false;
    }

}//end class